package com.ttbj.util;


import com.zywl.common.http.HttpClientUtil;
import com.zywl.common.http.HttpResultVO;
import com.zywl.common.spring.SpringContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;


/**
 * @description YMall httpclient工具
 * @author Daniel
 * @date 2016年5月6日 下午2:58:56
 * 
 */
public class HttpClientUtilYH {

    private static Logger logger = LoggerFactory.getLogger(HttpClientUtilYH.class);

    /**
     * 获取yinhu common HttpClientUtil
     * 
     * @author Daniel
     * @date 2016年5月6日 下午3:00:16
     * 
     * @return
     * @throws Exception
     */
    private static HttpClientUtil getHttpClientUtil() throws Exception {
        HttpClientUtil httpClientUtil = SpringContextHolder.getBean("httpClientUtil");
        if (httpClientUtil != null) {
            return httpClientUtil;
        } else {
            httpClientUtil = new HttpClientUtil();
            return httpClientUtil;
        }
    }

    /**
     * post
     * 
     * @author Daniel
     * @date 2016年5月6日 下午3:09:09
     * 
     * @param url
     * @param paras
     * @return
     * @throws IOException
     */
    public static String doPost(String url, Map<String, String> paras) throws IOException {
        String rs = null;
        long currentTime = System.currentTimeMillis();
        try {
            HttpResultVO httpResultVO = getHttpClientUtil().doPostDetail(url, null, paras, "utf-8");
            if (httpResultVO.getStatusCode() != null && httpResultVO.getStatusCode() == 200) {
                rs = httpResultVO.getResponseBody();
            } else {
                long lastTime = System.currentTimeMillis();
                logger.error("HTTP POST 请求URL({})发生非200返回,用时{}ms", url, lastTime - currentTime);
                return httpResultVO.getResponseBody();
            }
        } catch (Exception e) {
            long lastTime = System.currentTimeMillis();
            logger.error("HTTP POST 请求URL({})发生异常,用时{}ms ", url, lastTime - currentTime, e);
            throw new IOException(e.getMessage());
        }
        long lastTime = System.currentTimeMillis();
        logger.info("HTTP POST 请求URL({})正确,用时{}ms", url, lastTime - currentTime);
        return rs;
    }

    /**
     * get
     * 
     * @author Daniel
     * @date 2016年5月6日 下午3:09:14
     * 
     * @param url
     * @param paras
     * @return
     * @throws IOException
     */
    public static String doGet(String url, Map<String, String> paras) throws IOException {
        String rs = null;
        long currentTime = System.currentTimeMillis();
        try {
            HttpResultVO httpResultVO = getHttpClientUtil().doGetDetail(url, null, paras, "utf-8");
            if (httpResultVO.getStatusCode() != null && httpResultVO.getStatusCode() == 200) {
                rs = httpResultVO.getResponseBody();
            } else {
                long lastTime = System.currentTimeMillis();
                logger.error("HTTP POST 请求URL({})发生非200返回,用时{}ms", url, lastTime - currentTime);
                return httpResultVO.getResponseBody();
            }
        } catch (Exception e) {
            long lastTime = System.currentTimeMillis();
            logger.error("HTTP POST 请求URL({})发生异常,用时{}ms ", url, lastTime - currentTime, e);
            throw new IOException(e.getMessage());
        }
        long lastTime = System.currentTimeMillis();
        logger.info("HTTP POST 请求URL({})正确,用时{}ms", url, lastTime - currentTime);
        return rs;
    }

    public static String doPostStringDetail(String url, String json) throws IOException {
        String rs = null;
        long currentTime = System.currentTimeMillis();
        try {
            HttpResultVO httpResultVO = getHttpClientUtil().doPostStringDetail(url, null, json, "utf-8", 60000, 60000);
            if (httpResultVO.getStatusCode() != null && httpResultVO.getStatusCode() == 200) {
                rs = httpResultVO.getResponseBody();
            } else {
                long lastTime = System.currentTimeMillis();
                logger.error("HTTP POST 请求URL({})发生非200返回,用时{}ms", url, lastTime - currentTime);
                return httpResultVO.getResponseBody();
            }
        } catch (Exception e) {
            long lastTime = System.currentTimeMillis();
            logger.error("HTTP POST 请求URL({})发生异常,用时{}ms ", url, lastTime - currentTime, e);
            throw new IOException(e.getMessage());
        }
        long lastTime = System.currentTimeMillis();
        logger.info("HTTP POST 请求URL({})正确,用时{}ms", url, lastTime - currentTime);
        return rs;
    }

}
